-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add absolute links support #1802
base: master
Are you sure you want to change the base?
Add absolute links support #1802
Conversation
9374e52
to
addf00d
Compare
Hey, Is there any update on this one? This fixes the exact issue im having. Would be awesome if we could get this merged/looked at again. Thanks |
I can fix the merge conflict. Though, I still need someone to review/merge it. |
You'd be an actual hero if you did, thank you. I replied on the issue thread too so hopefully someone notices. |
Yes, this is long overdue. I currently have to resort to a templating engine to handle absolute paths. I use absolute paths almost exclusively as they don't require massive editing when I move a chapter up/down folder levels. |
addf00d
to
d9c5f10
Compare
Bump @ehuss |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
I think it is a little unfortunate to need to add a new config setting for this, but I don't see much of a way around it.
This will need some tests to be added.
It looks like links on the print page aren't using the adjusted links.
It also looks like there are some merge conflicts.
src/utils/mod.rs
Outdated
pub fn render_markdown_with_path( | ||
text: &str, | ||
curly_quotes: bool, | ||
path: Option<&Path>, | ||
abs_url: Option<&String>, | ||
) -> String { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a public API, it can't be changed without a semver break. I think as a workaround, there will need to be another function added which takes the new parameter, and render_markdown_with_path
can call it with None
. We can then drop the new (or old) function in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, I also left a comment to merge those functions in the future.
src/utils/mod.rs
Outdated
} | ||
.into(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what the .into()
is here for, but I don't think it is necessary?
} | |
.into(); | |
}; |
src/utils/mod.rs
Outdated
fixed_link = match abs_url { | ||
Some(abs_url) => format!("{}{}", abs_url.trim_end_matches('/'), &fixed_link), | ||
None => fixed_link, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a minor opinion on style, I think it might be a little clearer to use an if let
instead of a match
. It is 1 less line and makes it a little clearer that there is "no change" when the value is None
.
fixed_link = match abs_url { | |
Some(abs_url) => format!("{}{}", abs_url.trim_end_matches('/'), &fixed_link), | |
None => fixed_link, | |
} | |
if let Some(abs_url) = abs_url { | |
fixed_link = format!("{}{}", abs_url.trim_end_matches('/'), &fixed_link); | |
} |
/// Prepend the `site_url` in links with absolute path. | ||
pub use_site_url_as_root: bool, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, so technically adding this is a breaking change since HtmlConfig
is in the public API. However, we have been adding new fields to this struct for a while now, and nobody has complained. That is something we should definitely fix in the future, but for now I guess we can let it slide. 😦
@@ -159,6 +159,7 @@ The following configuration options are available: | |||
navigation links and script/css imports in the 404 file work correctly, even when accessing | |||
urls in subdirectories. Defaults to `/`. If `site-url` is set, | |||
make sure to use document relative links for your assets, meaning they should not start with `/`. | |||
- **use-site-url-as-root:** Prepend the `site_url` in links with absolute path. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add this to the TOML summary up above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, done,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
links with absolute path
As a user, I would wonder "what links are those"? It could be
- links generated by templates, such as those referencing style sheets and JavaScript code
- links generated between pages of the book
- links I insert using Markdown syntax
fixed_link.push_str(&caps["link"].trim_start_matches('/')); | ||
fixed_link.push_str(".html"); | ||
if let Some(anchor) = caps.name("anchor") { | ||
fixed_link.push_str(anchor.as_str()); | ||
} | ||
} else if !fixed_link.is_empty() { | ||
// prevent links with double slashes | ||
fixed_link.push_str(&dest.trim_start_matches('/')); | ||
} else { | ||
fixed_link.push_str(&dest); | ||
}; | ||
return CowStr::from(fixed_link); | ||
if dest.starts_with('/') || path.is_some() { | ||
if let Some(abs_url) = abs_url { | ||
fixed_link = format!( | ||
"{}/{}", | ||
abs_url.trim_end_matches('/'), | ||
&fixed_link.trim_start_matches('/') | ||
); | ||
} | ||
} | ||
return CowStr::from(format!("{}", fixed_link)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I needed to add changes to this function because of leading double slashes when the path
and abs_url
var are defined. I've added some tests to make sure it only affects those cases.
text: &str, | ||
curly_quotes: bool, | ||
path: Option<&Path>, | ||
abs_url: Option<&String>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The &String
type should normally not be used — &str
is more flexible for the callers since they can pass in both string literals (that would simplify the tests below) and borrowed owned strings.
Would it not be better to add a syntax to let links use the I'm thinking of something like To back to [the root]($path_to_root/index.md) The |
site-url value in book.toml did not work for me as far as I tried with mdbook v0.4.29
I can not rely on base href to solve all links and references use cases, dots in paths does not always help. To solve this I made some changes in: Changes can be seen in this fork (updated to v0.4.31) : master...JesusPerez:mdBook:master Obviously to complete the absolute paths in the pages I use a variable {{baseurl}} as a prefix of the href and src links. [Introduction]({{urlbase}}introduction.md)
![Image]({{urlbase}}image.jpg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code looks good to me, and the tests run, but unfortunately I wasn't able to actually confirm on the test_book the repo has that this PR fixes the issue. Is there some issue introduced in the latest commits that breaks this?
Confirmed here as well. Not working here: https://github.com/calvinbrewer/mdBook/tree/add_absolute_links_support with the configuration added as reference |
Sorry If I'm saying something wrong, but I opened this PR more than a year ago. |
I've added an example in the test_book In addition, from a usability perspective, I'd assume the left nav items to also include the |
I have successfully built your branch, and the HTML output from your example seems to be producing the expected results. In ....
<a href="/docs/prefix.html">This</a> should link to prefix.md.
.... IMO, this outcome aligns with our assumptions. Could you please share your build results? Maybe I missed something.
My intention with this particular changes was not to address all those cases. Regarding the navigation links, they are generated based on |
Hmm. I've tried doing this by myself, and after you wrote a response I checked again with the exact inputs you've provided. Here's a patch that I'm got for testing: diff --git a/test_book/book.toml b/test_book/book.toml
index a305007..91e8c9d 100644
--- a/test_book/book.toml
+++ b/test_book/book.toml
@@ -9,6 +9,8 @@ edition = "2018"
[output.html]
mathjax-support = true
+site-url = "/docs/"
+use-site-url-as-root = true
[output.html.playground]
editable = true
diff --git a/test_book/src/individual/README.md b/test_book/src/individual/README.md
index 67a1e9d..fceb065 100644
--- a/test_book/src/individual/README.md
+++ b/test_book/src/individual/README.md
@@ -2,6 +2,8 @@
This contains following tags:
+[Prefix](/prefix.md)
+
- Headings
- Paragraphs
- Line breaks I then run |
@KFearsoff to view the results, it is necessary to execute the command You can check it here: Line 67 in c1d622e
|
Oh. Ohhh. Yeah, I can confirm this works. This is... pretty unfortunate, though. I think |
@KFearsoff, I believe this constitutes a reasonable usage. The serve mode is designed exclusively for development purposes and is tasked solely with hosting the book files. I agree that enhanced documentation would be beneficial in this context though. Another aspect to consider carefully is the content of It would be good to have more feedbacks on it before any change. |
Just in case if it helps I updated my mdBook with absolute links via site-url based in current mdBook master branch (v0.4.40 f480534) I only fixed a couple of files, maybe it is not enough but it seems to work for me. You can clone it, build and try following how-to-use-it notes with sitefix-book as example. For instance I have tested absolute URL with hostnames and schemas and it works ! I have to deal with multi-books services with auth, for me it is critical to support absolute links. Let me know if I can help on this. |
Prepend the
site-url
in links with absolute path.Problem:
If we host in the book in a sub page we lost the references for the relative root. We already have the
site-url
in configs, but we do not use it. If we want to link a page in the root of the book, we need to prepend a lot of../
to reach the actual root, and if we move pages these relative refs could lead us to dead links.Proposal
Add a new flag
use-site-url-as-root
in theHtmlConfig
.The changes should have backward compatibility, we'll need to enable the
use-site-url-as-root
flag in[output.html]
section inside thebook.toml
file to use the new feature.TODO:
Closes: #1764